﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Cn.Edwon.Web {
    public class InfoModel {

        private string _Title;
        public string Title {
            set { _Title = value; }
            get { return _Title; }
        }

        private string _Content;
        public string Content {
            set { _Content = value; }
            get { return _Content; }
        }

        private string _RefUrl;
        public string RefUrl {
            set { _RefUrl = value; }
            get { return _RefUrl; }
        }

        private string _Redirect;
        public string Redirect {
            set { _Redirect = value; }
            get { return _Redirect; }
        }

        private string _Mode;
        public string Mode {
            set { _Mode = value; }
            get { return _Mode; }
        }

        private string _CallBack;
        public string CallBack {
            set { _CallBack = value; }
            get { return _CallBack; }
        }

    }
    public class SiteInfo {
        public static void Send(Page p, InfoModel eo) {
            p.ClientScript.RegisterClientScriptBlock(p.GetType(), p.Request.Url.AbsoluteUri, Msg2Json(eo), true);
        }
        public static void SendSucceed(Page p, string redirect) {
            Send(p, InfoText.SUCCEED, InfoText.SUCCEED,redirect);
        }
        public static void SendFailed(Page p, string redirect) {
            Send(p, InfoText.FAILED, InfoText.FAILED,redirect);
        }
        public static void Send(Page p, string title, string content) {
            InfoModel eo = new InfoModel();
            eo.Title = title;
            eo.Content = content;
            eo.CallBack = "";
            eo.Mode = "alert";
            eo.Redirect = "";
            eo.RefUrl = p.Request.Url.AbsoluteUri;
            Send(p, eo);
        }
        public static void Send(Page p, string title, string content, string redirect) {
            InfoModel eo = new InfoModel();
            eo.Title = title;
            eo.Content = content;
            eo.CallBack = "";
            eo.Mode = "alert";
            eo.Redirect = redirect;
            eo.RefUrl = p.Request.Url.AbsoluteUri;
            Send(p, eo);
        }
        public static void Send(Page p, string title, string content, string redirect,string mode) {
            InfoModel eo = new InfoModel();
            eo.Title = title;
            eo.Content = content;
            eo.CallBack = "";
            eo.Mode = (mode=="confirm")?"alert":"confirm";
            eo.Redirect = redirect;
            eo.RefUrl = p.Request.Url.AbsoluteUri;
            Send(p, eo);
        }
        public static string Msg2Json(InfoModel eo) {
            string js = "";
            js += "var BULTER_MSG = {\"Title\":\"" + FixStr(eo.Title) + "\"";
            js += ",\"Content\":\"" + FixStr(eo.Content) + "\"";
            js += ",\"RefUrl\":\"" + FixStr(eo.RefUrl) + "\"";
            js += ",\"Redirect\":\"" + FixStr(eo.Redirect) + "\"";
            js += ",\"Mode\":\"" + FixStr(eo.Mode) + "\"";
            js += ",\"CallBack\":\"" + FixStr(eo.CallBack) + "\"";
            js += "};";
            return js;
        }
        public static string Msg2Js(InfoModel eo) {
            string js = "";
            js += "jskitBulter.msg.load(\"" + FixStr(eo.Title) + "\"";
            js += ",\"" + FixStr(eo.Content) + "\"";
            js += ",\"" + FixStr(eo.RefUrl) + "\"";
            js += ",\"" + FixStr(eo.Redirect) + "\"";
            js += ",\"" + FixStr(eo.Mode) + "\"";
            js += ",\"" + FixStr(eo.CallBack) + "\"";
            js += ");";
            return js;
        }

        public static string FixStr(string str) {
            if (String.IsNullOrEmpty(str)) {
                return "";
            } else {
                str = str.Replace("\"", "\\\"");
                str = str.Replace("\r\n", "");
                str = str.Replace("\n", "");
                return str;
            }
        }
    }

    public class InfoText {
        public static readonly string INFO = "消息";
        public static readonly string OK = "确定";
        public static readonly string CANCEL = "取消";
        public static readonly string SUCCEED = "操作成功";
        public static readonly string FAILED = "操作失败";
        public static readonly string STANDBY = "请等待...";
        public static readonly string LOADING = "数据装载中...";
    }
}
